/**
 * @author Akos Szabo - Axi - aaszabo@gmail.com
 * Nov 26, 2011
 * 
 * Displays the list of task with the main arguments in a JTable.
 */
package com.axi.resplan.gui;

//Own imports
import com.axi.resplan.*;


//Java imports
import javax.swing.*;
import java.util.*;
import java.text.Format;
import java.text.SimpleDateFormat;

/**
 * @author Akos Szabo - Axi
 *
 */
public class TaskTablePanel extends JTable 
{
	
	private TimeTable _tt;
	
/**
 * 
 * Set header 
 * TableColumn method setHeaderValue	
 */
	
	public TaskTablePanel()
	{
		this(7,100);
	}
	
	
	public TaskTablePanel(TimeTable argTimeTable)
	{		
		this(7,argTimeTable.getTaskList().size()+1);
		
		System.out.println("task num:"+argTimeTable.getTaskList().size());
		
		this.addTimeTable(argTimeTable);
		updateView();
	}
	
	
	public TaskTablePanel(int argSizeX, int argSizeY)
	{
		super(argSizeY, argSizeX);
		//structura
		// #, name, startDate, endDate, length, resourcelist
		
		//rows, columns
/*		
		super(new String[][] { {"Feladat 1", "2011-11-11", "2011-12-31", "5", "1,2,3"},
				{"Feladat 1", "2011-11-11", "2011-12-31", "5", "1,2,3"},				
				{"Feladat 2", "2011-11-11", "2011-12-31", "5", "1,2,3"},				
				{"Feladat 3", "2011-11-11", "2011-12-31", "5", "1,2,3"},				
				{"Feladat 4", "2011-11-11", "2011-12-31", "5", "1,2,3"},				
				{"Feladat 5", "2011-11-11", "2011-12-31", "5", "1,2,3"},				
				{"Feladat 6", "2011-11-11", "2011-12-31", "5", "1,2,3"},				
				{"Feladat 7", "2011-11-11", "2011-12-31", "5", "1,2,3"},				
				{"Feladat 8", "2011-11-11", "2011-12-31", "5", "1,2,3"},				
				{"Feladat 9", "2011-11-11", "2011-12-31", "5", "1,2,3"}				
		},
                new String[] {"Task", "Start", "End", "Duration", "Resources"});
		//add(new JLabel("TaskTablePanel"));
		 
*/
		
	}//end of constructor
	
	public void addTimeTable( TimeTable argTable)
	{
		_tt = argTable;
	}
	
	/**
	 * load data to TimeTable
	 * Then update the view of data in the table view
	 * 
	 */
	public void loadData()
	{
		_tt.loadData();
		updateView();
	}
 
	/**
	 * Update the view in the JTable
	 * 
	 */
	private void updateView()
	{
		//clear old content
		resetView();
		
		Format formatter = new SimpleDateFormat("yyyy.MM.dd."); //.HH.mm.ss
		//s = formatter.format(date);
		
		
		
		//fill out the table with the data of _tt (TimeTable)
		Hashtable tl = _tt.getTaskList();
		Vector tv = _tt.getTaskVector();
		int i=0;
		//Enumeration en = tl.elements();
		Enumeration en = tv.elements();
		while(en.hasMoreElements())
		{
			//System.out.println(i);
			Task t = (Task)en.nextElement();
			this.setValueAt(t.getTaskId(), i, 0);
			this.setValueAt(t.getName(), i, 1);
			this.setValueAt(""+formatter.format(t.getEarliestStart()), i, 2);			
			this.setValueAt(""+formatter.format(t.getLatestEnd()), i, 3);
			this.setValueAt(""+t.getLengthInHur(), i, 4);
			Hashtable ht = t.getResources();
			
			String str="";
			Enumeration en2 = ht.elements();
			while(en2.hasMoreElements())
			{
				str+=""+(String)en2.nextElement()+", ";
			}
			
			this.setValueAt(str.substring(0, str.length()-2), i, 5);
			
			i++;
			
		}
		
	}
	
	
	
	private void resetView()
	{
		//use native JTable methodes
		this.selectAll();
		this.clearSelection();
	}
}
